	.section .data
Errorsocket: .asciz"socket() failed \n"
Econnect: .asciz"error connecting socket \n"
request:  .ascii "GET /n09230945.asp HTTP/1.1\r\nHost: automation.whatismyip.com\r\nUser-Agent: assembly language\r\nConnection: close\r\n\r\n"

.equ length, request
file: .asciz"iplogs.txt"
.section .bss
buffer: .space 4096 
fd: .space 4
bef: .space 1
.section .text

.globl _start
_start:
       #Creating a reliable, stream socket  TCP
       #parametersi
        movl $2, (%esp) #af_inet
        movl $1, 4(%esp) #sock-stream
        movl $0, 8(%esp) #ipprotocp

        movl $102,%eax #socketcall
        movl $1,%ebx   #socketcall_socket
        movl %esp,%ecx #ESP: stack address In esp we save all the parameters in the stack. Al tener un puntero a la pila, agarra todos los paarmetros de ahi  
        #the  pointer to the syscall args to %ecx.
        int  $0x80
        cmpl $0, %eax
        jl error

        #Establish a connection to the server
        # connect (int fd, struct sockaddr *uservaddr, int addrlen)        
        #parameters sent to esp
	 movl %eax, (%esp) #fd
        #struct sockaddrr *uservaddr 
        movw $2, 12(%esp) #af_inet
        movw $0x5000, 14(%esp) #port
        movl $0xc559e948, 16(%esp) # ip

         #connect()
         leal 12(%esp), %ebx #loading sockaddr *uservaddr pointer. 
         movl %ebx, 4(%esp) #we move the pointer after fd, that is to say 4
         movl $16, 8(%esp)  #size  
         movl    $102,%eax     #socketcall
         movl    $3, %ebx     #connect
         movl %esp, %ecx
         int    $0x80
         cmpl $0, %eax
         jne error2
          
          #send()
         leal request, %eax
         movl %eax, 4(%esp)          
         movl $length, 8(%esp)
         movl $0, 12(%esp)
         movl $102, %eax
         movl $9, %ebx  #send  
         movl %esp, %ecx
         int $0x80
         testl %eax, %eax
         jns goodsend
         cmpl $0, %eax #ver aca, tiene que ser comparado con -1
         jl error         
     
goodsend:         
         #receive()
while:   cmpl $0, %eax
         je endwhile
         movl $buffer, 4(%esp)
         movl $4095, 8(%esp)
         movl $0, 12(%esp)
         movl $102, %eax
         movl $10, %ebx
         movl %esp, %ecx
         int $0x80
                 
         jmp while 

endwhile: #see if it is 0 testl eax, %eax
          
          leal buffer,%edi
          leal buffer, %esi
          movl $16, %ecx #size  
          cld
          movl $0, bef
 repeathis: lodsb
            cmp $10, %al
            je equal
            movl $0, bef
            jmp repeathis      
 equal:     lodsb
            cmp $13, %al 
            je salto
            jmp repeathis         
          jmp endprogram 

salto:   lodsb 
          lodsb
          movl $0, %ecx
           
repetir:  #the problem is that it saves al reves, i have to fix that. 
cmpb $0, %al
          je space
          mov %al,(%esp)
          incl %ecx
          subl $1, %esp
          lodsb
          
         #addl $4, (%esp)
          #lodsl
          #movl %eax, (%esp)
     #     lodsb
      #    mov %al, 2(%esp)
       #   lodsb
        #  mov %al, 3(%esp)
         # lodsb
         # incl %ecx
          jmp repetir
 space:   
         movl $5, %eax #mode=5 open
                  movl $file, %ebx #destiny file
                  movl $02101, %ecx #write mode
                  int $0x80  #s.o call
  movl %eax, fd   #move eax to fd to avoid losing fd
              
                #write in another file the data copied                 
	         movl $4, %eax  
                 movl fd, %ebx  #fd must be in ebx
		 movl $15, %edx #bytes to be copied 
		 movl %esp, %ecx #data to be copied
		 int $0x80 #s.0 call       
         jmp endprogram
error:   pushl $Errorsocket
         call printf
         jmp  endprogram

error2:  pushl %eax
         pushl $Econnect
         call printf 
         jmp endprogram
endprogram:  
        movl $1, %eax
 	movl $0, %ebx
 	int $0x80 
